package com.example.myapplication.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.example.myapplication.BaseActivity;
import com.example.myapplication.R;
import com.example.myapplication.database.User;
import com.example.myapplication.util.SpUtil;
import com.example.myapplication.util.ToastUtil;

import org.litepal.LitePal;

import java.util.List;

public class LoginAty extends BaseActivity {
    private View btRegis,btLogin,btForget;
    private EditText etPhone,etPwd;

    @Override
    protected void initView() {
        btRegis= (View) findViewById(R.id.btRegis);
        btLogin=findViewById(R.id.btLogin);
        btForget=findViewById(R.id.btForget);
        etPhone= ((EditText) findViewById(R.id.etPhone));
        etPwd=findViewById(R.id.etPwd);


    }

    @Override
    protected void setListener() {
        super.setListener();
        btRegis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginAty.this,RegisActivity.class));
            }
        });
        btForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginAty.this, RegisActivity.class);
                intent.putExtra("isChange",true);
                startActivity(intent);

            }
        });
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<User> users = LitePal.where("phone = '" + etPhone.getText().toString() + "'").find(User.class);
                if (users.isEmpty()){
                    //无用户
                    ToastUtil.toast("无该用户请前往注册");
                }else {
                    //有用户
                    if (users.get(0).getPwd().equals(etPwd.getText().toString())){
                        //
                        ToastUtil.toast("登录成功");
                        SpUtil.saveLoginStatus(true);
                        SpUtil.saveLoginPhone(etPhone.getText().toString());
                        startActivity(new Intent(LoginAty.this,HomeAty.class));
                        finish();
                    }else {
                        ToastUtil.toast("密码错误");

                    }
                }

            }
        });
    }

    @Override
    protected void logicStart() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_login;
    }

}